﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OSGeo.MapGuide;
using MapViewer;

namespace MapViewerTest
{
    public partial class Form1 : Form, IMapStatusBar
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            viewer.MapActiveToolChanged += new EventHandler(OnActiveToolChanged);
            new MapViewerController(viewer, legend, this);
        }

        void OnActiveToolChanged(object sender, EventArgs e)
        {
            btnPan.Checked = (viewer.ActiveTool == MapActiveTool.Pan);
        }

        public void SetCursorPositionMessage(string message)
        {
            lblCoords.Text = message;
        }

        public void SetFeatureSelectedMessage(string message)
        {
            lblSelection.Text = message;
        }

        public void SetMapScaleMessage(string message)
        {
            lblScale.Text = message;
        }

        public void SetMapSizeMessage(string message)
        {
            lblSize.Text = message;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            viewer.RefreshMap(true);
        }

        private void btnZoomExtents_Click(object sender, EventArgs e)
        {
            viewer.InitialMapView();   
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            var map = viewer.GetMap();
            var scale = map.ViewScale * 1.2;
            map.SetViewScale(scale);
            viewer.RefreshMap(true);
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {
            var map = viewer.GetMap();
            var scale = map.ViewScale * 0.8;
            map.SetViewScale(scale);
            viewer.RefreshMap(true);
        }

        private void openMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var resId = new MgResourceIdentifier("Library://Samples/Sheboygan/Maps/Sheboygan.MapDefinition");
            var map = new MgdMap(resId);

            viewer.Init(map);
            viewer.RefreshMap(true);
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void viewer_BusyStateChanged(object sender, EventArgs e)
        {
            var busy = viewer.IsBusy;
            btnZoomExtents.Enabled = btnZoomIn.Enabled = btnZoomOut.Enabled = btnRefresh.Enabled = !busy;
        }

        private void btnPan_Click(object sender, EventArgs e)
        {
            viewer.ActiveTool = MapActiveTool.Pan;   
        }
    }
}
